<?php
class Default_Model_DbTable_ObjectPhotoProfile extends Zend_Db_Table_Abstract {
	protected $_name = 'object_photo_profile';
	public function addObjectPhotoProfile($object_id, $object_lib_photo_id, $name, $short_description, $long_description, $img, $status) {
		$data = array (
				'OBJECT_ID' => $object_id,
				'OBJECT_LIB_PHOTO_ID' => $object_lib_photo_id,
				'NAME' => $name,
				'SHORT_DESCRIPTION' => $short_description,
				'LONG_DESCRIPTION' => $long_description,
				'IMG' => $img,
				'STATUS' => $status 
		);
		return $this->insert ( $data );
	}
	public function updateObjectPhotoProfile($id, $object_id, $object_lib_photo_id, $name, $short_description, $long_description, $img, $status) {
		$data = array (
				'OBJECT_ID' => $object_id,
				'OBJECT_LIB_PHOTO_ID' => $object_lib_photo_id,
				'NAME' => $name,
				'SHORT_DESCRIPTION' => $short_description,
				'LONG_DESCRIPTION' => $long_description,
				'IMG' => $img,
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function removeObjectPhotoProfile($id) {
		return $this->delete ( "ID = $id" );
	}
	public function activeObjectPhotoProfile($id) {
		$lv = $this->fetchRow ( "ID = $id" );
		if ($lv ["STATUS"] == 1) {
			$this->update ( array (
					"STATUS" => 0 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => 1 
			), "ID = $id" );
		}
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function getObjectPhotoProfileByObjectId($object_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_photo_profile AS opp" )->where ( "opp.OBJECT_ID= $object_id" )->group ( "opp.ID" )->join ( "object AS o", "o.ID = opp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	
	public function getPhotoProfileByObjectId($object_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
	
		$rows = NULL;
	
		$select->from ( "object_photo_profile AS opp" )->where ( "opp.OBJECT_ID= $object_id" )->join ( "object AS o", "o.ID = opp.OBJECT_ID", array (
				"o.OBJECT_NAME"
		) );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	
	
	public function getObjectPhotoProfileByObjectName($object_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_photo_profile AS opp", array (
				"opp.ID",
				"opp.NAME",
				"opp.IMG",
				"opp.SHORT_DESCRIPTION",
				"opp.LONG_DESCRIPTION",
				"opp.MODE_SLIDE",
				"opp.STATUS" 
		) )->where ( "opp.STATUS = ?", 1 )->join ( "object AS o", "o.ID = opp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->join ( "object_lib_photo AS olp", "olp.ID = opp.object_lib_photo_id", array (
				"olp.NAME" 
		) )->where ( "o.OBJECT_NAME= ?", $object_name );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectPhotoProfileByLibPhotoId($lib_photo_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		try {
			$rows = $this->fetchAll ( "OBJECT_LIB_PHOTO_ID = $lib_photo_id AND STATUS = 1" );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectPhotoProfileByLibPhotoName($lib_photo_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_photo_profile AS opp", array (
				"opp.ID",
				"opp.NAME",
				"opp.IMG",
				"opp.SHORT_DESCRIPTION",
				"opp.LONG_DESCRIPTION",
				"opp.MODE_SLIDE",
				"opp.STATUS" 
		) )->where ( "opp.STATUS = ?", 1 )->join ( "object AS o", "o.ID = opp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->join ( "object_lib_photo AS olp", "olp.ID = opp.object_lib_photo_id", array (
				"olp.NAME" 
		) )->where ( "olp.NAME= ?", $lib_photo_name );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getAllObjectPhotoProfile($o_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = null;
		
		if($o_id == "!="){
			$select->from("object_photo_profile as opp")
			->where("opp.STATUS = 1 and opp.OBJECT_ID != '' and o.DETAIL != 'Trang chủ'")
					->joinLeft("object as o", "o.ID = opp.OBJECT_ID",array("o.OBJECT_NAME","o.DETAIL"))
					->joinLeft("object_lib_photo AS olp", "olp.ID = opp.OBJECT_LIB_PHOTO_ID",array("olp.NAME"))
					->group("opp.ID")
					->order("opp.ID DESC");
		}else{
			$select->from("object_photo_profile as opp")
			->where("opp.STATUS = 1 and opp.OBJECT_LIB_PHOTO_ID != ''")
			->joinLeft("object_lib_photo AS olp", "olp.ID = opp.OBJECT_LIB_PHOTO_ID",array("olp.NAME"))
			->group("opp.ID")
			->order("opp.ID DESC");
		}
		try {
			$rows = $this->fetchAll($select)->toArray();#print_r($rows);die();
		} catch (Exception $e) {
			print_r($e);
		}
		return $rows;
	}
	public function getById($id){
		return $this->fetchRow("ID = $id");
	}
	public function getObjectPhotoProfileById($id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from("object_photo_profile as opp")
			->where("opp.STATUS = 1 and  opp.ID = $id")
			->join("object_lib_photo AS olp", "olp.ID = opp.OBJECT_LIB_PHOTO_ID",array("olp.NAME"));
		try {
			$rows = $this->fetchRow ( $select );#print_r($rows);die("deubg");
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectPhotoProfileByName($name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object_photo_profile AS opp", array (
				"opp.ID",
				"opp.NAME",
				"opp.IMG",
				"opp.SHORT_DESCRIPTION",
				"opp.LONG_DESCRIPTION",	
				"opp.STATUS" 
		) )->where ( "opp.STATUS = ?", 1 )->join ( "object AS o", "o.ID = opp.OBJECT_ID", array (
				"o.OBJECT_NAME" 
		) )->join ( "object_lib_photo AS olp", "olp.ID = opp.object_lib_photo_id", array (
				"olp.NAME" 
		) )->where ( "opp.NAME= ?", $name );
		try {
			$rows = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}

}

